package edu.scau.divineplace.controller;

import edu.scau.divineplace.config.global.log.WebLog;
import edu.scau.divineplace.entity.query.AccountQuery;
import edu.scau.divineplace.entity.validate.group.Update;
import edu.scau.divineplace.entity.vo.AccountVO;
import edu.scau.divineplace.service.AccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;


/**
 * @Author: x1aolone
 * @Date: 2020/12/11 15:47
 */
@Slf4j
@RestController
@CrossOrigin
@Api("Account控制器")
@WebLog("Account控制器")
@Validated
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @ApiOperation("通过用户id获取的用户信息")
    @GetMapping("/{userId}")
    public AccountVO get(@PathVariable Long userId){
        return accountService.get(userId);
    }

    @ApiOperation("通过用户id修改用户信息")
    @PutMapping("/{userId}")
    public void update(@PathVariable Long userId, @Validated(Update.class) AccountQuery query){
        accountService.update(userId, query);
    }

    @ApiOperation("登录, 若成功登录返回用户信息")
    @GetMapping("/login")
    public AccountVO login(@NotBlank String telephone, @NotBlank String password){
        return accountService.login(telephone, password);
    }

    @ApiOperation("注册, 若注册成功返回用户信息")
    @PostMapping("/register")
    public AccountVO register(@Validated AccountQuery query){
        return accountService.register(query);
    }


}
